<template>
  <div class="home">
    <van-nav-bar class="page-nav-bar" title="搜素">
      <template #title>
        <van-button to="/search" class="search-btn" icon="search" type="primary"
          >搜索</van-button
        >
      </template>
    </van-nav-bar>

    <!-- 标签 -->
    <van-tabs
      sticky
      offset-top="1.22rem"
      class="home-tab"
      v-model="active"
      animated
      swipeable
    >
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <article-list :channelsId="item.id"></article-list>
      </van-tab>

      <template #nav-right>
        <div class="placeholder-box"></div>
        <div class="right-menu" @click="isShowEditPopup = true">
          <i class="toutiao toutiao-gengduo"></i>
        </div>
      </template>
    </van-tabs>
    <van-popup
      v-model="isShowEditPopup"
      position="bottom"
      :style="{ height: '80%' }"
      closeable
      close-icon-position="top-left"
      class="editPopup"
    >
      <channel-edit
        @setActice="active--"
        :channels="channels"
        :active="active"
        @onGo="onGo"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserSelChannels } from '@/api/user.js'
import ArticleList from './components/ArticleList'
import ChannelEdit from './components/ChannelEdit'
import { Toast } from 'vant'
import { getStorage } from '@/utils/storage.js'
export default {
  name: 'home',
  data () {
    return {
      active: 0,
      channels: [],
      isShowEditPopup: false
    }
  },
  components: {
    ArticleList,
    ChannelEdit
  },

  created () {
    this.getUserChannels()
  },

  methods: {
    async getUserChannels () {
      try {
        const storage = getStorage('MY-CHANNELS')
        if (!this.$store.state.user && storage) {
          this.channels = storage
        } else {
          const res = await getUserSelChannels()
          console.log(res)
          this.channels = res.channels
        }
      } catch (error) {
        Toast('Channels获取失败！')
      }
    },
    onGo (e) {
      this.active = e
      this.isShowEditPopup = false
    }
  }
}
</script>

<style scoped lang="less">
.home {
  .search-btn {
    width: 555px;
    height: 64px;
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 32px;
  }
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .van-icon-search {
    color: #fff;
  }
  /deep/.home-tab {
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      height: 82px;
      .van-tab__text {
        font-size: 28px;
        color: #777;
      }
    }
    .van-tabs__wrap {
      height: 82px;
      border-bottom: 1px solid #edeff3;
      .van-tabs__line {
        background-color: #3296fa;
        width: 31px;
        height: 6px;
        border-right: 3px;
        bottom: 8px;
      }
      .van-tabs__nav--line {
        padding: 0;
      }
    }
    .van-tab--active {
      .van-tab__text {
        font-size: 30px;
        color: #333;
      }
    }
    .right-menu {
      width: 66px;
      height: 82px;
      background-color: #ffffff;
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      // border-left: 1px solid #edeff3;
      // border-bottom: 1px solid #edeff3;
      box-sizing: border-box;
      i {
        font-size: 40px;
      }
      &::before {
        content: '';
        width: 2px;
        height: 58px;
        position: absolute;
        left: 0;
        top: 12px;
        background-image: url(~@/assets/img/gradient-gray-line.png);
        background-size: 100% 100%;
      }
    }
    .placeholder-box {
      width: 66px;
      flex-shrink: 0;
    }
  }
  .editPopup {
    padding-top: 100px;
  }
}
</style>
